package cn.db101.api.complex.component

import cn.db101.api.complex.api.weather.WeatherPropertiesConfig
import cn.hutool.extra.spring.SpringUtil
import io.github.mymonstercat.Model
import io.github.mymonstercat.ocr.InferenceEngine
import io.minio.MinioClient
import net.sourceforge.tess4j.Tesseract
import org.mybatis.spring.annotation.MapperScan
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.web.servlet.MultipartConfigFactory
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
import org.springframework.http.converter.HttpMessageConverter
import org.springframework.web.method.support.HandlerMethodArgumentResolver
import org.springframework.web.servlet.config.annotation.CorsRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
import java.io.File
import javax.servlet.MultipartConfigElement


/**
 *
 * @author：zooooooooy
 * @date: 2022/4/21 - 18:25
 */
@Configuration
@ConditionalOnClass(WebMvcConfigurer::class)
@MapperScan("cn.db101.api.complex.mapper")
@Import(SpringUtil::class)
@EnableConfigurationProperties(*[ApiComplexPropertiesConfig::class, WeatherPropertiesConfig::class])
open class ApiComplexConfig : WebMvcConfigurer {

    @Autowired
    lateinit var converters: List<HttpMessageConverter<*>>

    @Autowired
    lateinit var minioConfigProperties: cn.db101.api.complex.component.MinioConfigProperties

    @Value("\${file.basePath}")
    var basePath: String? = null

    override fun addArgumentResolvers(resolvers: MutableList<HandlerMethodArgumentResolver>) {
        resolvers.add(cn.db101.api.complex.component.RequestDataArgument(converters))
    }

    override fun addCorsMappings(registry: CorsRegistry) {
        registry.addMapping("/**")
            .allowedHeaders(
                "Content-Type",
                "X-Requested-With",
                "accept,Origin",
                "Access-Control-Request-Method",
                "Access-Control-Request-Headers",
                "token",
                "authorization",
                "Access-Control-Allow-Credentials",
                "Cache-Control",
                "x-token"
            )
            .allowedOriginPatterns("*")
            .allowedMethods("*").allowCredentials(true)
    }

    @Bean
    open fun multipartConfigElement(): MultipartConfigElement {
        val factory = MultipartConfigFactory()
        //2.该处就是指定的路径(需要提前创建好目录，否则上传时会抛出异常)
        factory.setLocation(basePath)
        val file = File(basePath)
        file.mkdirs()
        return factory.createMultipartConfig()
    }

    @Bean
    open fun minioClient(): MinioClient {

        return MinioClient.builder()
            .endpoint(minioConfigProperties.endpoint)
            .credentials(minioConfigProperties.accessKey, minioConfigProperties.secretKey)
            .build()
    }

    @Bean
    open fun tesseract(): Tesseract {
        return Tesseract().also {
            it.setLanguage("chi_sim")
            it.setDatapath("src/main/resources/tessdata")
        }
    }

    @Bean
    open fun engine(): InferenceEngine {
        return InferenceEngine.getInstance(Model.ONNX_PPOCR_V3)
    }

}
